package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /*
    * 添加购物车
    * @param shoppingCartDTO
    * */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 查询当前是否存在该购物车数据
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        cart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        // 如果存在，则更新数量
        if(list != null && list.size() > 0){
            ShoppingCart cartInDB = list.get(0);
            cartInDB.setNumber(cartInDB.getNumber() + 1);
            shoppingCartMapper.update(cartInDB);


        }else{
            // 如果不存在，则添加
            //判断添加进来的是菜品还是套餐
            if(shoppingCartDTO.getDishId() != null){
                // 添加的是菜品
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());

            }else {
                // 添加的是套餐
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
                cart.setAmount(setmeal.getPrice());

            }
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(cart);
        }

    }


    /*
    * 查看购物车
    * @return
    * */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        return list;
    }

    /*
    * 清空购物车
    * */
    @Override
    public void clean() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    /*
    * 删除购物车
    * @param shoppingCartDTO
    * */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, cart);
        cart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        if(list != null && list.size() > 0){
            ShoppingCart cartInDB = list.get(0);
            Integer number = cartInDB.getNumber();
            if(number == 1){
                shoppingCartMapper.deleteById(cartInDB.getId());
            }else{
                cartInDB.setNumber(number - 1);
                shoppingCartMapper.update(cartInDB);
            }
        }
    }
}
